var UserBag = app.controller('UserBag', function($rootScope, $scope, $http,$state,$stateParams,UserBagData) {
	try{
		$rootScope.showf=false;
		$rootScope.title = '钱包/提现';
		$scope.busy = false;
		$scope.show = false;
		$scope.charge=false;
		$scope.currentPage = 1;
		$scope.limit = 10;
		$scope.pages = 1;
		$scope.pay = "";

		$scope.id=$stateParams.id;
		var t=$stateParams.t;

		$scope.user={};
		//诚信预约金
		$scope.pledge=0;

		//获取用户信息
		var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=userinfo";
		$http.jsonp(url).success(function(data) {
			$scope.user=data.retval;
		});
		
		var url=SITE_URL+"index.php?app=basetutorapp&callback=JSON_CALLBACK&act=dongjie";
		$http.jsonp(url).success(function(data) {
			$scope.pledge=data.retval;
		});

		//获取历史充值
		var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=charge";
		$http.jsonp(url).success(function(data) {
			if(data.done&&data.retval!=undefined&&data.retval!=""){
				$scope.pay=data.retval.charge_type;
				if($scope.pay==undefined||$scope.pay==null||$scope.pay==""){
					$scope.pay="微信";
				}
			}else{
				$scope.pay="微信";
			}
		});

		$scope.m=0;
		$scope.d=0;
		$scope.cha=0;
		//获取诚信预约金
		if($scope.id>0&&t>0){
			/*var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=pledge&t="+t;
			$http.jsonp(url).success(function(data) {
				if(data.done){
					$scope.pledge=data.retval.a;

					$scope.m=data.retval.m;
					$scope.d=data.retval.d;
					$scope.cha=data.retval.cha;

					if($scope.cha<0){
						$scope.cha=-$scope.cha;
						$scope.show=true;
					}
				}
			});*/
		}

		$scope.fastcharge=function(){
			$scope.show=false;
			$scope.charge=true;
			$scope.mm=$scope.cha;
		}

		//获取钱包明细
		var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=money";
		$http.jsonp(url).success(function(data) {
			if(data.done){
				$scope.money=data.retval.data;
				$scope.pages = Math.ceil(data.retval.total/$scope.limit);
			}
		});
		
		$scope.aid=0;
		//获取现金券使用规则文章
		var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=coin";
		$http.jsonp(url).success(function(data) {
			if(data.done){
				$scope.aid=data.retval;
			}
		});

		//关闭支付
		$scope.close=function(){
			try{
				$scope.show = false;
				$rootScope.back();
			}
			catch (e) {
				showcatch(e);
			}
		}

		//加载更多明细
		$scope.loadMore = function() {
			try{
				if ($scope.currentPage < $scope.pages) {

					if ($scope.busy) {
						return false; 
					}
					$scope.busy = true;
					$scope.currentPage++;

					var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=money&page="+$scope.currentPage;
					$http.jsonp(url).success(function(data) {
						for (var i in data.retval.data) {
							if(typeof data.retval.data[i]!="function")
								$scope.money.push(data.retval.data[i]);
						}
						$scope.pages = Math.ceil(data.retval.total/$scope.limit);

						$scope.busy = false;
						if($scope.pages<=$scope.currentPage){
							//$scope.alerts = [{type : 'success' , msg : '没有更多内容了'}]
						}
					});
				}
			}
			catch (e) {
				showcatch(e);
			}
		};
		
		$scope.keyPress=function(e){
			if(e.which==13||e.which==9){
				$scope.docharge();
			}
		}

		$scope.docharge=function(){
			if(parseFloat($scope.mm)<=0){
				showtoast('请输入充值金额');
				return;
			}

			if($scope.pay=='微信'){
				$scope.wxcharge();
			}else if($scope.pay=='支付宝'){
				$scope.alicharge();
			}
		}

		$scope.wxlogintimer = null;
		$scope.alicharge=function(){
			try{
				try{
					$scope.model = $cordovaDevice.getModel();
					$scope.platform = $cordovaDevice.getPlatform();
					$scope.uuid = $cordovaDevice.getUUID();
				}
				catch(ex){
					$scope.model = 'web';
					$scope.platform = 'web1';
					$scope.uuid = '12312312312';
				}

				var url=SITE_URL+"index.php?app=usercharge&callback=JSON_CALLBACK&act=chargealipay&m="+$scope.mm+"&model="+$scope.model+"&platform="+$scope.platform+"&uuid="+$scope.uuid;
				$http.jsonp(url).success(function(data) {
					if(data.done){
						$scope.charge_id=data.retval.charge_id;
						ProgressIndicator.showSimpleWithLabel(false,'正在启动支付宝...');
						try{
							$scope.wxlogintimer = window.setTimeout("ProgressIndicator.hide()", 10000);
							alipay.payment(data.retval, function cb_success(result){
								if ($scope.wxlogintimer != null)
									window.clearTimeout($scope.wxlogintimer);
								ProgressIndicator.hide();
								result=JSON.parse(result);
								if(result.resultStatus=="9000"){
									var url=SITE_URL+"index.php?app=payhelper&callback=JSON_CALLBACK&act=aliquery&t=charge&sn=charge_"+$scope.charge_id;
									$http.jsonp(url).success(function(data){});

									var url=SITE_URL+"index.php?app=usercharge&callback=JSON_CALLBACK&act=chargealipayok&"+result.result+"&model="+$scope.model+"&platform="+$scope.platform+"&uuid="+$scope.uuid;
									$http.jsonp(url).success(function(data) {
										if(data.done){
											showtoast('充值成功');
											if($scope.id>0&&t>0){
												$rootScope.back();
											}else{
												$state.reload();
											}
										}
									});
								}
								else if(result.resultStatus=="8000"){
									showtoast("充值结果确认中");
								}
								else {
									showtoast(result.memo+result.resultStatus);
								}
							}, function cb_failure(result){
								if ($scope.wxlogintimer != null)
									window.clearTimeout($scope.wxlogintimer);
								ProgressIndicator.hide();
								showtoast(result);
							});
						}
						catch(e){
							showtoast('充值失败'+e.message);
							ProgressIndicator.hide();
						}
					}
				});
			}
			catch(e){
				showtoast(e.message);
			}
		};

		$scope.wxcharge=function(){
			try{
				try{
					$scope.model = $cordovaDevice.getModel();
					$scope.platform = $cordovaDevice.getPlatform();
					$scope.uuid = $cordovaDevice.getUUID();
				}
				catch(ex){
					$scope.model = 'web';
					$scope.platform = 'web1';
					$scope.uuid = '12312312312';
				}
				var url=SITE_URL+"index.php?app=usercharge&callback=JSON_CALLBACK&act=chargewxpay&m="+$scope.mm+"&model="+$scope.model+"&platform="+$scope.platform+"&uuid="+$scope.uuid;
				$http.jsonp(url).success(function(data) {
					if(data.done){
						if(data.retval.return_code!='SUCCESS')
						{
							showtoast(data.retval.return_msg);
							return;
						}
						$scope.charge_id=data.retval.charge_id;
						console.log(data.retval);
						ProgressIndicator.showSimpleWithLabel(false,'正在启动微信支付...');
						try{
							$scope.wxlogintimer = window.setTimeout("ProgressIndicator.hide()", 10000);
							Wechat.payment({'appid': data.retval.appid,//公众账号ID,
								'noncestr': data.retval.nonce_str,//随机字符串,
								'package': data.retval.package,//扩展字段,
								'partnerid': data.retval.mch_id,//商户号,
								'prepayid': data.retval.prepay_id,//预支付交易会话ID,
								'timestamp': data.retval.timestamp,//时间戳,
								'sign': data.retval.sign//签名
							}, function (pluginResult){
								re=JSON.parse(pluginResult);
								if(re.code==0){
									var url=SITE_URL+"index.php?app=payhelper&callback=JSON_CALLBACK&act=wxquery&t=charge&sn=charge_"+$scope.charge_id;
									$http.jsonp(url).success(function(data){});

									var url=SITE_URL+"index.php?app=usercharge&callback=JSON_CALLBACK&act=chargewxpayok&charge_id="+$scope.charge_id+"&model="+$scope.model+"&platform="+$scope.platform+"&uuid="+$scope.uuid;
									$scope.charge_id=0;
									$http.jsonp(url).success(function(data) {
										if(data.done){
											if($scope.id>0&&t>0){
												$rootScope.back();
											}else{
												$state.reload();
											}
										}
									});
								}
								else{
									showtoast('充值失败');
								}
								console.log(pluginResult);
								if ($scope.wxlogintimer != null)
									window.clearTimeout($scope.wxlogintimer);
								ProgressIndicator.hide();
							}, function (pluginResult){
								if ($scope.wxlogintimer != null)
									window.clearTimeout($scope.wxlogintimer);
								ProgressIndicator.hide();
								re=JSON.parse(pluginResult);
								showtoast('充值失败'+re.code);//'{'errStr':'','code':-1}'
								console.log(pluginResult);
							});
						}
						catch(e){
							showtoast('充值失败'+e.message);
							ProgressIndicator.hide();
						}
						ProgressIndicator.hide();
					}
				});
			}
			catch(e){
				showtoast(e.message);
			}
		};
	}
	catch (e) {
		showcatch(e);
	}
});
UserBag.UserBagData=function($q, $http,$stateParams,$rootScope){
	try {
		var data = [];
		var def = $q.defer();
		if(window.localStorage.u==undefined||window.localStorage.u==null||window.localStorage.u==""){			
			$rootScope.go("loginmain");
		}else{
			def.resolve(data);
		}
		
		return def.promise;
	} catch (e) {
		showcatch(e);
	}
}